package com.mlk.engSystem.service.impl;

import com.mlk.constant.EngConstant;
import com.mlk.core.component.page.Page;
import com.mlk.core.component.page.Pager;
import com.mlk.core.exception.GlobalException;
import com.mlk.core.util.ServerResponse;
import com.mlk.core.util.SessionUtils;
import com.mlk.engSystem.mapper.EngSysEmpMapper;
import com.mlk.engSystem.mapper.EngSysEmpRemainApplyMapper;
import com.mlk.engSystem.mapper.EngSysEmpRemainLogMapper;
import com.mlk.engSystem.po.EngSysEmpRemainApply;
import com.mlk.engSystem.po.EngSysEmpRemainLog;
import com.mlk.engSystem.service.EngEmpRemainApplyService;
import com.mlk.engSystem.vo.EngSysEmpRemainApplyVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 业务员额度申请处理服务
 */
@Service
public class EngEmpRemainApplyServiceImpl implements EngEmpRemainApplyService {

    @Resource
    private EngSysEmpRemainApplyMapper engSysEmpRemainApplyMapper;

    @Resource
    private EngSysEmpMapper engSysEmpMapper;

    @Resource
    private EngSysEmpRemainLogMapper engSysEmpRemainLogMapper;

    @Override
    @Page
    public Pager listPage(EngSysEmpRemainApplyVO vo) {
        return Pager.adapt(this.engSysEmpRemainApplyMapper.selectForPage(vo));
    }

    @Override
    @Transactional
    public ServerResponse applyOK(EngSysEmpRemainApplyVO vo) {
        vo.setStatus(EngConstant.REMAIN_APPLY.OK_STATUS);
        vo.setVerifyName(SessionUtils.getUser().getSysUser().getName());
        vo.setVerifyTime(new Date());
        int count =  this.engSysEmpRemainApplyMapper.updateById(vo);
        if(count == 0){
            throw new GlobalException("操作失败");
        }
        EngSysEmpRemainApply apply = this.engSysEmpRemainApplyMapper.selectById(vo.getId());
        count = this.engSysEmpMapper.updateRemain(apply.getOpenId(),apply.getAmount());
        if(count == 0){
            throw new GlobalException("操作失败");
        }
        EngSysEmpRemainLog log = EngSysEmpRemainLog
                .builder()
                .openId(apply.getOpenId())
                .createTime(new Date())
                .createName(SessionUtils.getUser().getSysUser().getName())
                .amount(apply.getAmount())
                .type(EngConstant.EMP_REMAIN_LOG_TYPE.APPLY_LOG)
                .build();
        // 添加变更日志
        count = this.engSysEmpRemainLogMapper.insert(log);
        if(count == 0 ){
            throw new GlobalException("操作失败");
        }
        return ServerResponse.ok("操作成功");
    }

    @Override
    @Transactional
    public ServerResponse applyFail(EngSysEmpRemainApplyVO vo) {

        vo.setStatus(EngConstant.REMAIN_APPLY.FAIL_STATUS);
        vo.setVerifyName(SessionUtils.getUser().getSysUser().getName());
        vo.setVerifyTime(new Date());
        int count =  this.engSysEmpRemainApplyMapper.updateById(vo);

        if(count == 0){
            throw new GlobalException("操作失败");
        }
        return ServerResponse.ok("操作成功");
    }
}
